package ru.niir.goodfil.ui;

import ru.niir.goodfil.ui.models.TopProductModel;
import ru.niir.goodfil.ui.utils.excel.TopProductsUnload;

import java.io.IOException;
import java.math.BigDecimal;
import java.math.BigInteger;
import java.util.ArrayList;
import java.util.List;

public class TopProductsBean extends PageBean {

    private List<TopProductModel> topProductModels;

    public TopProductsBean() {
        init();
    }

    public void init(){
        initTopProductModels();
    }

    private void initTopProductModels() {
        topProductModels = new ArrayList<TopProductModel>();
        List list = gf().getTopFiftyProducts();
        for(Object o : list){
            Object[] arr = (Object[])o;
            String goodCode = (String) arr[0];
            int goodCount = (Integer)arr[1];
            int goodReservedCount = (Integer)arr[2];
            long sales = ((BigDecimal)arr[3]).longValue();
            long averageSalesPerMonth = sales/12;
            long stockCoef = goodCount/averageSalesPerMonth;
            StringBuilder stringBuilder = new StringBuilder();
            if(stockCoef < 1){
                stringBuilder.append("меньше месяца");
            }
            else {
                stringBuilder.append(stockCoef);
                stringBuilder.append(" месяц(а)");
            }
            TopProductModel productModel = new TopProductModel
            (
                goodCode,
                goodCount,
                goodReservedCount,
                averageSalesPerMonth,
                sales,
                stringBuilder.toString()
            );
            topProductModels.add(productModel);
        }
    }

    public void getExcel(){
        try {
            TopProductsUnload.topProductsListToExcel(topProductModels);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    public List<TopProductModel> getTopProductModels() {
        return topProductModels;
    }
}
